package net.wsfcp.android.ui.view.my.activity;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.wsfcp.android.R;
import net.wsfcp.android.adapter.OrderListAdapter;
import net.wsfcp.android.base.BaseActivity;
import net.wsfcp.android.bean.resultbean.OrderBean;
import net.wsfcp.android.bean.resultbean.enumInterface.PayType;
import net.wsfcp.android.http.mvp.contract.OrderListContract;
import net.wsfcp.android.event.ReloadOrderEvent;
import net.wsfcp.android.http.mvp.presenter.OrderDetailPresenter;
import net.wsfcp.android.ui.widget.RecycleViewDivider;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;


//订单详情界面
public class OrderDetailActivity extends BaseActivity<OrderDetailPresenter> implements OrderListContract.OrderDetailView {
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_right)
    Button btnRight;


    @BindView(R.id.txt_trade_num)
    TextView txt_trade_num;
    @BindView(R.id.txt_remark)
    TextView txt_remark;
    @BindView(R.id.txt_order_code)
    TextView txt_order_code;
    @BindView(R.id.txt_price_trade)
    TextView txt_price_trade;
    @BindView(R.id.txt_price_deliver)
    TextView txt_price_deliver;
    @BindView(R.id.txt_price_all)
    TextView txt_price_all;
    @BindView(R.id.txt_need_pay)
    TextView txt_need_pay;
    @BindView(R.id.txt_pay_time)
    TextView txt_pay_time;
    @BindView(R.id.txt_deliver_time)
    TextView txt_deliver_time;


    @BindView(R.id.txt_name)
    TextView txt_name;
    @BindView(R.id.txt_phone)
    TextView txt_phone;
    @BindView(R.id.txt_address)
    TextView txt_address;


    @BindView(R.id.txt_pay_way)
    TextView txt_pay_way;
    @BindView(R.id.txt_pay_status)
    TextView txt_pay_status;
    @BindView(R.id.txt_order_time)
    TextView txt_order_time;
    @BindView(R.id.txt_order_status)
    TextView txt_order_status;


    @BindView(R.id.txt_cancel)
    TextView txt_cancel;
    @BindView(R.id.txt_confirm)
    TextView txt_confirm;


    @BindView(R.id.rv_list)
    RecyclerView rv_list;

    OrderBean.OrderList bean;
    OrderListAdapter adapter;

    private int id;//订单id

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_detail;
    }


    @Override
    protected OrderDetailPresenter loadPresenter() {
        return new OrderDetailPresenter();
    }

    @Override
    protected void initData() {
        setmTitle(getString(R.string.my_order_detail));
        setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        mPresenter.getOrderDetail(id + "");
    }

    @Override
    protected void initListener() {
        txt_cancel.setOnClickListener(this);
        txt_confirm.setOnClickListener(this);
    }

    @Override
    protected void otherViewClick(View view) {
        switch (view.getId()) {
            case R.id.txt_confirm:

                new AlertDialog.Builder(this)
                        .setMessage(getResources().getString(R.string.order_hint_confirm)).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();

                        mPresenter.confirmReceipt(bean.getId() + "");

                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        dialogInterface.dismiss();
                    }
                }).create().show();

                break;
            case R.id.txt_cancel:
                new AlertDialog.Builder(this)
                        .setMessage(getResources().getString(R.string.order_hint_cancel)).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();

                        mPresenter.cancelOrder(bean.getId() + "");

                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        dialogInterface.dismiss();
                    }
                }).create().show();
                break;


        }


    }

    @Override
    protected void initView() {
        btnLeft = rlLeft;
        mTitle = txtTitle;
        btRight = btnRight;

        id = getIntent().getIntExtra("id", 0);


    }

    public void initReyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        rv_list.setLayoutManager(layoutManager);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        rv_list.addItemDecoration(new RecycleViewDivider(this, dp(8), dp(8), true));
    }

    public void initAdapter() {
        adapter = new OrderListAdapter(bean.getOrder2GoodsList(), this);
        rv_list.setAdapter(adapter);
    }

    public void initDetail() {
        txt_deliver_time.setText(bean.getShowDeliverTime());
        txt_pay_time.setText(bean.getShowPayTime());
        txt_trade_num.setText(bean.getOutTradeNo());
        txt_remark.setText(bean.getRemark());
        txt_order_code.setText(bean.getOrderNumber());
        txt_price_trade.setText(bean.getShowTradePrice() + "");
        txt_price_deliver.setText(bean.getShowFreight() + "");
        txt_price_all.setText(bean.getPayAmount() + "");
        txt_need_pay.setText(bean.getPayAmount() + "");

        txt_order_time.setText(bean.getShowCreateTime());


        String paytype = "";

        @PayType.Type int orderPayType = bean.getPayType();

        switch (orderPayType) {//0:微信 1:支付宝 2:银联 3:苹果IAP 4=货到付款
            case PayType.TYPE_WEIXINPAY:
                paytype = "微信支付";
                break;
            case PayType.TYPE_ALIPAY:
                paytype = "支付宝支付";
                break;
            case PayType.TYPE_UNIONPAY:
                paytype = "银联支付";
                break;
            case PayType.TYPE_IAP:
                paytype = "苹果IAP支付";
                break;
            case PayType.TYPE_CASH:
                paytype = "货到付款";
                break;

        }
        txt_pay_way.setText(paytype);


        String paystatus = "";
        switch (bean.getPayStatus()) {//0:未支付 1:已支付 2:支付失败 3:支付中 4:转入退款 5:已关闭
            case 0:
                paystatus = "未支付";
                break;
            case 1:
                paystatus = "已支付";
                break;
            case 2:
                paystatus = "付失败";
                break;
            case 3:
                paystatus = "支付中";
                break;
            case 4:
                paystatus = "转入退款";
                break;
            case 5:
                paystatus = "已关闭";
                break;

        }
        txt_pay_status.setText(paystatus);


        String orderstatus = "";
        switch (bean.getStatus()) {//-1:已取消 0:未处理 1:配送中 2:已完成 3:已评价
            case -1:
                orderstatus = "已取消";
                break;
            case 0:
                orderstatus = "未处理";
                break;
            case 1:
                orderstatus = "配送中";
                break;
            case 2:
                orderstatus = "已完成";
                break;
            case 3:
                orderstatus = "已评价";
                break;


        }
        txt_order_status.setText(orderstatus);


        txt_name.setText(bean.getReceiver());
        txt_phone.setText(bean.getCellNum());
        txt_address.setText(bean.getAddress());

        if (bean.getStatus() == 1) {
            txt_confirm.setVisibility(View.VISIBLE);
        } else {
            txt_confirm.setVisibility(View.INVISIBLE);
        }

        if (bean.isCanCancel()) {
            txt_cancel.setVisibility(View.VISIBLE);
        } else {
            txt_cancel.setVisibility(View.INVISIBLE);
        }


    }


    @Override
    public void cancelOrderSuccess() {
        toast("已取消订单");
        EventBus.getDefault().post(new ReloadOrderEvent());
        finish();

    }

    @Override
    public void cancelOrderFail(String failMsg) {
        toast(failMsg);
    }


    @Override
    public void getOrderDetailSuccess(OrderBean.OrderList orderBean) {
        bean = orderBean;
        initReyclerView();
        initAdapter();
        initDetail();
    }

    @Override
    public void getOrderDetailFail(String failMsg) {
        toast(failMsg);

    }

    @Override
    public void confirmReceiptSuccess() {
        toast("已确认收货");
        EventBus.getDefault().post(new ReloadOrderEvent());
        finish();
    }

    @Override
    public void confirmReceiptFail(String failMsg) {
        toast(failMsg);
    }
}
